package com.beyondbit.hr.core.dao;

import com.beyondbit.hr.api.dto.request.OrgUserQueryReq;
import com.beyondbit.hr.utils.mybatis.SQLUtils;
import org.apache.ibatis.builder.annotation.ProviderMethodResolver;
import org.apache.ibatis.jdbc.SQL;

import java.util.Map;

public class OrgUserSqlProvider implements ProviderMethodResolver {
    private final static String TABLE_NAME = "hr_org_user";

    public static String findOrgUser(final Map<String, Object> map){
        OrgUserQueryReq req = (OrgUserQueryReq)map.get("req");

        SQLUtils sqlUtils = new SQLUtils(map,"req");

        String sql =  new SQL(){{
            SELECT("*");
            FROM(TABLE_NAME);
            if (req.getOrgIds()!= null && req.getOrgIds().size() > 0){
                WHERE(sqlUtils.in("OrgId","orgIds"));
            }

            if (req.getUserGuids()!= null && req.getUserGuids().size() > 0){
                WHERE(sqlUtils.in("UserGuid","userGuids"));
            }

            if (req.getCreateTimeBegin() != null){
                WHERE(sqlUtils.greaterThanOrEqual("CreateTime","createTimeBegin"));
            }
            if (req.getCreateTimeEnd() != null){
                WHERE(sqlUtils.lessThanOrEqual("CreateTime","createTimeEnd"));
            }

            if (req.getMainOrg() != null){
                WHERE(sqlUtils.equals("isMainOrg","isMainOrg"));
            }

            if (req.getDelete() != null){
                WHERE(sqlUtils.equals("IsDelete","isDelete"));
            }

        }}.toString();

        return  sqlUtils.limitAndOffset(sql);
    }
}
